package P150;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class P49 {

    public boolean isValid(String s) {
        List<String> valid = new ArrayList<>();
        valid.add("{}");
        valid.add("[]");
        valid.add("()");
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (char aChar : chars) {
            if (stack.isEmpty()) {
                stack.push(aChar);
            } else {
                Character peek = stack.peek();
                if (valid.contains(String.valueOf(peek) + aChar)) {
                    stack.pop();
                } else {
                    stack.push(aChar);
                }
            }
        }
        return stack.isEmpty();
    }
}
